package homework.api.day04;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class SortList {
    public static void main(String[] args) {
        List<Integer> list1 = new ArrayList<>();
        list1.add(9);
        list1.add(6);
        list1.add(0);
        list1.add(2);
        list1.add(7);
        System.out.println("list1:"+list1);
        //集合帮助类
        Collections.sort(list1);
        System.out.println("排序后："+list1);

        List<String> list2 = new ArrayList<>();
        list2.add("张三");
        list2.add("法外狂徒");
        list2.add("abc");
        list2.add("6666");
        list2.add("qwertyuiop");
        System.out.println("list2:"+list2);
        //jdk1.8时，List集合自身提供了sort方法进行排序，方法中需要传入比较器
        list2.sort((o1,o2)->o1.length()-o2.length());
        System.out.println("排序后："+list2);

        List<Point> list3 = new ArrayList<>();
        list3.add(new Point(5,9));
        list3.add(new Point(12,65));
        list3.add(new Point(99,65));
        list3.add(new Point(20,34));
        list3.add(new Point(45,21));
        System.out.println("list3:"+list3);
        Collections.sort(list3,(o1,o2)->{
            int len1 = o1.getX()*o1.getX()+o1.getY()*o1.getY();
            int len2 = o2.getX()*o2.getX()+o2.getY()*o2.getY();
            return len1-len2;
        });

        System.out.println("排序后："+list3);
    }
}
